using Spine;
using System;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SkeletonRenderer : MonoBehaviour
{
	public delegate void SkeletonRendererDelegate(SkeletonRenderer skeletonRenderer);

	private class LastState
	{
		public struct AddSubmeshArguments
		{
			public Material material;

			public int startSlot;

			public int endSlot;

			public int triangleCount;

			public int firstVertex;

			public bool lastSubmesh;

			public AddSubmeshArguments(Material material, int startSlot, int endSlot, int triangleCount, int firstVertex, bool lastSubmesh)
			{
				this.material = material;
				this.startSlot = startSlot;
				this.endSlot = endSlot;
				this.triangleCount = triangleCount;
				this.firstVertex = firstVertex;
				this.lastSubmesh = lastSubmesh;
			}

			public bool Equals(ref AddSubmeshArguments other)
			{
				return !object.ReferenceEquals(material, null) && !object.ReferenceEquals(other.material, null) && material.GetInstanceID() == other.material.GetInstanceID() && startSlot == other.startSlot && endSlot == other.endSlot && triangleCount == other.triangleCount && firstVertex == other.firstVertex;
			}
		}

		public bool immutableTrianglesMesh1;

		public bool immutableTrianglesMesh2;

		public int vertexCount;

		public readonly ExposedList<bool> attachmentsFlipStateTemp = new ExposedList<bool>();

		public readonly ExposedList<bool> attachmentsFlipStateMesh1 = new ExposedList<bool>();

		public readonly ExposedList<bool> attachmentsFlipStateMesh2 = new ExposedList<bool>();

		public readonly ExposedList<int> attachmentsTriangleCountTemp = new ExposedList<int>();

		public readonly ExposedList<int> attachmentsTriangleCountMesh1 = new ExposedList<int>();

		public readonly ExposedList<int> attachmentsTriangleCountMesh2 = new ExposedList<int>();

		public readonly ExposedList<AddSubmeshArguments> addSubmeshArgumentsTemp = new ExposedList<AddSubmeshArguments>();

		public readonly ExposedList<AddSubmeshArguments> addSubmeshArgumentsMesh1 = new ExposedList<AddSubmeshArguments>();

		public readonly ExposedList<AddSubmeshArguments> addSubmeshArgumentsMesh2 = new ExposedList<AddSubmeshArguments>();
	}

	public SkeletonRendererDelegate OnReset;

	[NonSerialized]
	public bool valid;

	[NonSerialized]
	public Skeleton skeleton;

	public SkeletonDataAsset skeletonDataAsset;

	public string initialSkinName;

	public bool calculateNormals;

	public bool calculateTangents;

	public float zSpacing;

	public bool renderMeshes = true;

	public bool immutableTriangles;

	public bool frontFacing;

	public bool logErrors;

	[SpineSlot("", "", false)]
	public string[] submeshSeparators = new string[0];

	[HideInInspector]
	public List<Slot> submeshSeparatorSlots = new List<Slot>();

	private MeshRenderer meshRenderer;

	private MeshFilter meshFilter;

	private Mesh mesh1;

	private Mesh mesh2;

	private bool useMesh1;

	private float[] tempVertices = new float[8];

	private Vector3[] vertices;

	private Color32[] colors;

	private Vector2[] uvs;

	private Material[] sharedMaterials = new Material[0];

	private readonly ExposedList<Material> submeshMaterials = new ExposedList<Material>();

	private readonly ExposedList<Submesh> submeshes = new ExposedList<Submesh>();

	private SkeletonUtilitySubmeshRenderer[] submeshRenderers;

	private LastState lastState = new LastState();

	public virtual void Reset()
	{
		if (meshFilter != null)
		{
			meshFilter.sharedMesh = null;
		}
		meshRenderer = GetComponent<MeshRenderer>();
		if (meshRenderer != null)
		{
			meshRenderer.sharedMaterial = null;
		}
		if (mesh1 != null)
		{
			if (Application.isPlaying)
			{
				UnityEngine.Object.Destroy(mesh1);
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(mesh1);
			}
		}
		if (mesh2 != null)
		{
			if (Application.isPlaying)
			{
				UnityEngine.Object.Destroy(mesh2);
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(mesh2);
			}
		}
		lastState = new LastState();
		mesh1 = null;
		mesh2 = null;
		vertices = null;
		colors = null;
		uvs = null;
		sharedMaterials = new Material[0];
		submeshMaterials.Clear();
		submeshes.Clear();
		skeleton = null;
		valid = false;
		if (!skeletonDataAsset)
		{
			if (logErrors)
			{
				UnityEngine.Debug.LogError("Missing SkeletonData asset.", this);
			}
			return;
		}
		SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(quiet: false);
		if (skeletonData != null)
		{
			valid = true;
			meshFilter = GetComponent<MeshFilter>();
			meshRenderer = GetComponent<MeshRenderer>();
			mesh1 = newMesh();
			mesh2 = newMesh();
			vertices = new Vector3[0];
			skeleton = new Skeleton(skeletonData);
			if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default")
			{
				skeleton.SetSkin(initialSkinName);
			}
			submeshSeparatorSlots.Clear();
			for (int i = 0; i < submeshSeparators.Length; i++)
			{
				submeshSeparatorSlots.Add(skeleton.FindSlot(submeshSeparators[i]));
			}
			CollectSubmeshRenderers();
			LateUpdate();
			if (OnReset != null)
			{
				OnReset(this);
			}
		}
	}

	public void CollectSubmeshRenderers()
	{
		submeshRenderers = GetComponentsInChildren<SkeletonUtilitySubmeshRenderer>();
	}

	public virtual void Awake()
	{
		Reset();
	}

	public virtual void OnDestroy()
	{
		if (mesh1 != null)
		{
			if (Application.isPlaying)
			{
				UnityEngine.Object.Destroy(mesh1);
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(mesh1);
			}
		}
		if (mesh2 != null)
		{
			if (Application.isPlaying)
			{
				UnityEngine.Object.Destroy(mesh2);
			}
			else
			{
				UnityEngine.Object.DestroyImmediate(mesh2);
			}
		}
		mesh1 = null;
		mesh2 = null;
	}

	private Mesh newMesh()
	{
		Mesh mesh = new Mesh();
		mesh.name = "Skeleton Mesh";
		mesh.hideFlags = HideFlags.HideAndDontSave;
		mesh.MarkDynamic();
		return mesh;
	}

	public virtual void LateUpdate()
	{
		if (!valid || (!meshRenderer.enabled && submeshRenderers.Length == 0))
		{
			return;
		}
		int num = 0;
		int num2 = 0;
		int firstVertex = 0;
		int startSlot = 0;
		Material material = null;
		ExposedList<Slot> drawOrder = skeleton.drawOrder;
		int count = drawOrder.Count;
		int count2 = submeshSeparatorSlots.Count;
		bool flag = renderMeshes;
		ExposedList<int> attachmentsTriangleCountTemp = lastState.attachmentsTriangleCountTemp;
		attachmentsTriangleCountTemp.GrowIfNeeded(count);
		attachmentsTriangleCountTemp.Count = count;
		ExposedList<bool> attachmentsFlipStateTemp = lastState.attachmentsFlipStateTemp;
		attachmentsFlipStateTemp.GrowIfNeeded(count);
		attachmentsFlipStateTemp.Count = count;
		ExposedList<LastState.AddSubmeshArguments> addSubmeshArgumentsTemp = lastState.addSubmeshArgumentsTemp;
		addSubmeshArgumentsTemp.Clear(clearArray: false);
		for (int i = 0; i < count; i++)
		{
			Slot slot = drawOrder.Items[i];
			Bone bone = slot.bone;
			Attachment attachment = slot.attachment;
			bool flag2 = bone.worldScaleX >= 0f;
			bool flag3 = bone.worldScaleY >= 0f;
			bool flag4 = (flag2 && flag3) || (!flag2 && !flag3);
			bool flag5 = frontFacing && bone.worldFlipX != bone.worldFlipY == flag4;
			attachmentsFlipStateTemp.Items[i] = flag5;
			attachmentsTriangleCountTemp.Items[i] = -1;
			RegionAttachment regionAttachment = attachment as RegionAttachment;
			object rendererObject;
			int num3;
			int num4;
			if (regionAttachment != null)
			{
				rendererObject = regionAttachment.RendererObject;
				num3 = 4;
				num4 = 6;
			}
			else
			{
				if (!flag)
				{
					continue;
				}
				MeshAttachment meshAttachment = attachment as MeshAttachment;
				if (meshAttachment != null)
				{
					rendererObject = meshAttachment.RendererObject;
					num3 = meshAttachment.vertices.Length >> 1;
					num4 = meshAttachment.triangles.Length;
				}
				else
				{
					SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
					if (skinnedMeshAttachment == null)
					{
						continue;
					}
					rendererObject = skinnedMeshAttachment.RendererObject;
					num3 = skinnedMeshAttachment.uvs.Length >> 1;
					num4 = skinnedMeshAttachment.triangles.Length;
				}
			}
			Material material2 = (Material)((AtlasRegion)rendererObject).page.rendererObject;
			if ((material != null && material.GetInstanceID() != material2.GetInstanceID()) || (count2 > 0 && submeshSeparatorSlots.Contains(slot)))
			{
				addSubmeshArgumentsTemp.Add(new LastState.AddSubmeshArguments(material, startSlot, i, num2, firstVertex, lastSubmesh: false));
				num2 = 0;
				firstVertex = num;
				startSlot = i;
			}
			material = material2;
			num2 += num4;
			num += num3;
			attachmentsTriangleCountTemp.Items[i] = num4;
		}
		addSubmeshArgumentsTemp.Add(new LastState.AddSubmeshArguments(material, startSlot, count, num2, firstVertex, lastSubmesh: true));
		bool flag6 = CheckIfMustUpdateMeshStructure(attachmentsTriangleCountTemp, attachmentsFlipStateTemp, addSubmeshArgumentsTemp);
		if (flag6)
		{
			submeshMaterials.Clear();
			int j = 0;
			for (int count3 = addSubmeshArgumentsTemp.Count; j < count3; j++)
			{
				LastState.AddSubmeshArguments addSubmeshArguments = addSubmeshArgumentsTemp.Items[j];
				AddSubmesh(addSubmeshArguments.material, addSubmeshArguments.startSlot, addSubmeshArguments.endSlot, addSubmeshArguments.triangleCount, addSubmeshArguments.firstVertex, addSubmeshArguments.lastSubmesh, attachmentsFlipStateTemp);
			}
			if (submeshMaterials.Count == sharedMaterials.Length)
			{
				submeshMaterials.CopyTo(sharedMaterials);
			}
			else
			{
				sharedMaterials = submeshMaterials.ToArray();
			}
			meshRenderer.sharedMaterials = sharedMaterials;
		}
		Vector3[] array = vertices;
		bool flag7 = num > array.Length;
		if (flag7)
		{
			array = (vertices = new Vector3[num]);
			colors = new Color32[num];
			uvs = new Vector2[num];
			mesh1.Clear();
			mesh2.Clear();
		}
		else
		{
			Vector3 zero = Vector3.zero;
			int k = num;
			for (int vertexCount = lastState.vertexCount; k < vertexCount; k++)
			{
				array[k] = zero;
			}
		}
		lastState.vertexCount = num;
		float num5 = zSpacing;
		float[] array2 = tempVertices;
		Vector2[] array3 = uvs;
		Color32[] array4 = colors;
		int num6 = 0;
		float num7 = skeleton.a * 255f;
		float r = skeleton.r;
		float g = skeleton.g;
		float b = skeleton.b;
		Vector3 vector = default(Vector3);
		Vector3 a = default(Vector3);
		if (num == 0)
		{
			vector = new Vector3(0f, 0f, 0f);
			a = new Vector3(0f, 0f, 0f);
		}
		else
		{
			vector.x = 2.14748365E+09f;
			vector.y = 2.14748365E+09f;
			a.x = -2.14748365E+09f;
			a.y = -2.14748365E+09f;
			if (num5 > 0f)
			{
				vector.z = 0f;
				a.z = num5 * (float)(count - 1);
			}
			else
			{
				vector.z = num5 * (float)(count - 1);
				a.z = 0f;
			}
			int num8 = 0;
			Color32 color = default(Color32);
			do
			{
				Slot slot2 = drawOrder.Items[num8];
				Attachment attachment2 = slot2.attachment;
				RegionAttachment regionAttachment2 = attachment2 as RegionAttachment;
				if (regionAttachment2 != null)
				{
					regionAttachment2.ComputeWorldVertices(slot2.bone, array2);
					float z = (float)num8 * num5;
					float num9 = array2[0];
					float num10 = array2[1];
					float num11 = array2[2];
					float num12 = array2[3];
					float num13 = array2[4];
					float num14 = array2[5];
					float num15 = array2[6];
					float num16 = array2[7];
					array[num6].x = num9;
					array[num6].y = num10;
					array[num6].z = z;
					array[num6 + 1].x = num15;
					array[num6 + 1].y = num16;
					array[num6 + 1].z = z;
					array[num6 + 2].x = num11;
					array[num6 + 2].y = num12;
					array[num6 + 2].z = z;
					array[num6 + 3].x = num13;
					array[num6 + 3].y = num14;
					array[num6 + 3].z = z;
					color.a = (byte)(num7 * slot2.a * regionAttachment2.a);
					color.r = (byte)(r * slot2.r * regionAttachment2.r * (float)(int)color.a);
					color.g = (byte)(g * slot2.g * regionAttachment2.g * (float)(int)color.a);
					color.b = (byte)(b * slot2.b * regionAttachment2.b * (float)(int)color.a);
					if (slot2.data.blendMode == BlendMode.additive)
					{
						color.a = 0;
					}
					array4[num6] = color;
					array4[num6 + 1] = color;
					array4[num6 + 2] = color;
					array4[num6 + 3] = color;
					float[] array5 = regionAttachment2.uvs;
					array3[num6].x = array5[0];
					array3[num6].y = array5[1];
					array3[num6 + 1].x = array5[6];
					array3[num6 + 1].y = array5[7];
					array3[num6 + 2].x = array5[2];
					array3[num6 + 2].y = array5[3];
					array3[num6 + 3].x = array5[4];
					array3[num6 + 3].y = array5[5];
					if (num9 < vector.x)
					{
						vector.x = num9;
					}
					else if (num9 > a.x)
					{
						a.x = num9;
					}
					if (num11 < vector.x)
					{
						vector.x = num11;
					}
					else if (num11 > a.x)
					{
						a.x = num11;
					}
					if (num13 < vector.x)
					{
						vector.x = num13;
					}
					else if (num13 > a.x)
					{
						a.x = num13;
					}
					if (num15 < vector.x)
					{
						vector.x = num15;
					}
					else if (num15 > a.x)
					{
						a.x = num15;
					}
					if (num10 < vector.y)
					{
						vector.y = num10;
					}
					else if (num10 > a.y)
					{
						a.y = num10;
					}
					if (num12 < vector.y)
					{
						vector.y = num12;
					}
					else if (num12 > a.y)
					{
						a.y = num12;
					}
					if (num14 < vector.y)
					{
						vector.y = num14;
					}
					else if (num14 > a.y)
					{
						a.y = num14;
					}
					if (num16 < vector.y)
					{
						vector.y = num16;
					}
					else if (num16 > a.y)
					{
						a.y = num16;
					}
					num6 += 4;
				}
				else
				{
					if (!flag)
					{
						continue;
					}
					MeshAttachment meshAttachment2 = attachment2 as MeshAttachment;
					if (meshAttachment2 != null)
					{
						int num17 = meshAttachment2.vertices.Length;
						if (array2.Length < num17)
						{
							array2 = (tempVertices = new float[num17]);
						}
						meshAttachment2.ComputeWorldVertices(slot2, array2);
						color.a = (byte)(num7 * slot2.a * meshAttachment2.a);
						color.r = (byte)(r * slot2.r * meshAttachment2.r * (float)(int)color.a);
						color.g = (byte)(g * slot2.g * meshAttachment2.g * (float)(int)color.a);
						color.b = (byte)(b * slot2.b * meshAttachment2.b * (float)(int)color.a);
						if (slot2.data.blendMode == BlendMode.additive)
						{
							color.a = 0;
						}
						float[] array6 = meshAttachment2.uvs;
						float z2 = (float)num8 * num5;
						int num18 = 0;
						while (num18 < num17)
						{
							float num19 = array2[num18];
							float num20 = array2[num18 + 1];
							array[num6].x = num19;
							array[num6].y = num20;
							array[num6].z = z2;
							array4[num6] = color;
							array3[num6].x = array6[num18];
							array3[num6].y = array6[num18 + 1];
							if (num19 < vector.x)
							{
								vector.x = num19;
							}
							else if (num19 > a.x)
							{
								a.x = num19;
							}
							if (num20 < vector.y)
							{
								vector.y = num20;
							}
							else if (num20 > a.y)
							{
								a.y = num20;
							}
							num18 += 2;
							num6++;
						}
						continue;
					}
					SkinnedMeshAttachment skinnedMeshAttachment2 = attachment2 as SkinnedMeshAttachment;
					if (skinnedMeshAttachment2 == null)
					{
						continue;
					}
					int num21 = skinnedMeshAttachment2.uvs.Length;
					if (array2.Length < num21)
					{
						array2 = (tempVertices = new float[num21]);
					}
					skinnedMeshAttachment2.ComputeWorldVertices(slot2, array2);
					color.a = (byte)(num7 * slot2.a * skinnedMeshAttachment2.a);
					color.r = (byte)(r * slot2.r * skinnedMeshAttachment2.r * (float)(int)color.a);
					color.g = (byte)(g * slot2.g * skinnedMeshAttachment2.g * (float)(int)color.a);
					color.b = (byte)(b * slot2.b * skinnedMeshAttachment2.b * (float)(int)color.a);
					if (slot2.data.blendMode == BlendMode.additive)
					{
						color.a = 0;
					}
					float[] array7 = skinnedMeshAttachment2.uvs;
					float z3 = (float)num8 * num5;
					int num22 = 0;
					while (num22 < num21)
					{
						float num23 = array2[num22];
						float num24 = array2[num22 + 1];
						array[num6].x = num23;
						array[num6].y = num24;
						array[num6].z = z3;
						array4[num6] = color;
						array3[num6].x = array7[num22];
						array3[num6].y = array7[num22 + 1];
						if (num23 < vector.x)
						{
							vector.x = num23;
						}
						else if (num23 > a.x)
						{
							a.x = num23;
						}
						if (num24 < vector.y)
						{
							vector.y = num24;
						}
						else if (num24 > a.y)
						{
							a.y = num24;
						}
						num22 += 2;
						num6++;
					}
				}
			}
			while (++num8 < count);
		}
		Mesh mesh = (!useMesh1) ? mesh2 : mesh1;
		meshFilter.sharedMesh = mesh;
		mesh.vertices = array;
		mesh.colors32 = array4;
		mesh.uv = array3;
		if (flag6)
		{
			int num25 = mesh.subMeshCount = submeshMaterials.Count;
			for (int l = 0; l < num25; l++)
			{
				mesh.SetTriangles(submeshes.Items[l].triangles, l);
			}
		}
		Vector3 vector2 = a - vector;
		Vector3 center = vector + vector2 * 0.5f;
		mesh.bounds = new Bounds(center, vector2);
		if (flag7 && calculateNormals)
		{
			Vector3[] array8 = new Vector3[num];
			Vector3 vector3 = new Vector3(0f, 0f, -1f);
			for (int m = 0; m < num; m++)
			{
				array8[m] = vector3;
			}
			((!useMesh1) ? mesh1 : mesh2).vertices = array;
			mesh1.normals = array8;
			mesh2.normals = array8;
			if (calculateTangents)
			{
				Vector4[] array9 = new Vector4[num];
				Vector3 v = new Vector3(0f, 0f, 1f);
				for (int n = 0; n < num; n++)
				{
					array9[n] = v;
				}
				mesh1.tangents = array9;
				mesh2.tangents = array9;
			}
		}
		ExposedList<int> exposedList;
		ExposedList<LastState.AddSubmeshArguments> exposedList2;
		ExposedList<bool> exposedList3;
		if (useMesh1)
		{
			exposedList = lastState.attachmentsTriangleCountMesh1;
			exposedList2 = lastState.addSubmeshArgumentsMesh1;
			exposedList3 = lastState.attachmentsFlipStateMesh1;
			lastState.immutableTrianglesMesh1 = immutableTriangles;
		}
		else
		{
			exposedList = lastState.attachmentsTriangleCountMesh2;
			exposedList2 = lastState.addSubmeshArgumentsMesh2;
			exposedList3 = lastState.attachmentsFlipStateMesh2;
			lastState.immutableTrianglesMesh2 = immutableTriangles;
		}
		exposedList.GrowIfNeeded(attachmentsTriangleCountTemp.Capacity);
		exposedList.Count = attachmentsTriangleCountTemp.Count;
		attachmentsTriangleCountTemp.CopyTo(exposedList.Items, 0);
		exposedList3.GrowIfNeeded(attachmentsFlipStateTemp.Capacity);
		exposedList3.Count = attachmentsFlipStateTemp.Count;
		attachmentsFlipStateTemp.CopyTo(exposedList3.Items, 0);
		exposedList2.GrowIfNeeded(addSubmeshArgumentsTemp.Count);
		exposedList2.Count = addSubmeshArgumentsTemp.Count;
		addSubmeshArgumentsTemp.CopyTo(exposedList2.Items);
		if (submeshRenderers.Length > 0)
		{
			for (int num26 = 0; num26 < submeshRenderers.Length; num26++)
			{
				SkeletonUtilitySubmeshRenderer skeletonUtilitySubmeshRenderer = submeshRenderers[num26];
				if (skeletonUtilitySubmeshRenderer.submeshIndex < sharedMaterials.Length)
				{
					skeletonUtilitySubmeshRenderer.SetMesh(meshRenderer, (!useMesh1) ? mesh2 : mesh1, sharedMaterials[skeletonUtilitySubmeshRenderer.submeshIndex]);
				}
				else
				{
					skeletonUtilitySubmeshRenderer.GetComponent<Renderer>().enabled = false;
				}
			}
		}
		useMesh1 = !useMesh1;
	}

	private bool CheckIfMustUpdateMeshStructure(ExposedList<int> attachmentsTriangleCountTemp, ExposedList<bool> attachmentsFlipStateTemp, ExposedList<LastState.AddSubmeshArguments> addSubmeshArgumentsTemp)
	{
		if (immutableTriangles != ((!useMesh1) ? lastState.immutableTrianglesMesh2 : lastState.immutableTrianglesMesh1))
		{
			return true;
		}
		ExposedList<int> exposedList;
		ExposedList<LastState.AddSubmeshArguments> exposedList2;
		ExposedList<bool> exposedList3;
		if (useMesh1)
		{
			exposedList = lastState.attachmentsTriangleCountMesh1;
			exposedList2 = lastState.addSubmeshArgumentsMesh1;
			exposedList3 = lastState.attachmentsFlipStateMesh1;
		}
		else
		{
			exposedList = lastState.attachmentsTriangleCountMesh2;
			exposedList2 = lastState.addSubmeshArgumentsMesh2;
			exposedList3 = lastState.attachmentsFlipStateMesh2;
		}
		int count = attachmentsTriangleCountTemp.Count;
		if (exposedList.Count != count)
		{
			return true;
		}
		for (int i = 0; i < count; i++)
		{
			if (exposedList.Items[i] != attachmentsTriangleCountTemp.Items[i])
			{
				return true;
			}
		}
		for (int j = 0; j < count; j++)
		{
			if (exposedList3.Items[j] != attachmentsFlipStateTemp.Items[j])
			{
				return true;
			}
		}
		int count2 = addSubmeshArgumentsTemp.Count;
		if (exposedList2.Count != count2)
		{
			return true;
		}
		for (int k = 0; k < count2; k++)
		{
			if (!exposedList2.Items[k].Equals(ref addSubmeshArgumentsTemp.Items[k]))
			{
				return true;
			}
		}
		return false;
	}

	private void AddSubmesh(Material material, int startSlot, int endSlot, int triangleCount, int firstVertex, bool lastSubmesh, ExposedList<bool> flipStates)
	{
		int count = submeshMaterials.Count;
		submeshMaterials.Add(material);
		if (submeshes.Count <= count)
		{
			submeshes.Add(new Submesh());
		}
		else if (immutableTriangles)
		{
			return;
		}
		Submesh submesh = submeshes.Items[count];
		int[] array = submesh.triangles;
		int num = array.Length;
		if (lastSubmesh && num > triangleCount)
		{
			for (int i = triangleCount; i < num; i++)
			{
				array[i] = 0;
			}
			submesh.triangleCount = triangleCount;
		}
		else if (num != triangleCount)
		{
			array = (submesh.triangles = new int[triangleCount]);
			submesh.triangleCount = 0;
		}
		if (!renderMeshes && !frontFacing)
		{
			if (submesh.firstVertex != firstVertex || submesh.triangleCount < triangleCount)
			{
				submesh.triangleCount = triangleCount;
				submesh.firstVertex = firstVertex;
				int num2 = 0;
				int num3 = 0;
				while (num3 < triangleCount)
				{
					array[num3] = firstVertex;
					array[num3 + 1] = firstVertex + 2;
					array[num3 + 2] = firstVertex + 1;
					array[num3 + 3] = firstVertex + 2;
					array[num3 + 4] = firstVertex + 3;
					array[num3 + 5] = firstVertex + 1;
					num3 += 6;
					firstVertex += 4;
					num2++;
				}
			}
			return;
		}
		ExposedList<Slot> drawOrder = skeleton.DrawOrder;
		int j = startSlot;
		int num4 = 0;
		for (; j < endSlot; j++)
		{
			Slot slot = drawOrder.Items[j];
			Attachment attachment = slot.attachment;
			bool flag = flipStates.Items[j];
			if (attachment is RegionAttachment)
			{
				if (!flag)
				{
					array[num4] = firstVertex;
					array[num4 + 1] = firstVertex + 2;
					array[num4 + 2] = firstVertex + 1;
					array[num4 + 3] = firstVertex + 2;
					array[num4 + 4] = firstVertex + 3;
					array[num4 + 5] = firstVertex + 1;
				}
				else
				{
					array[num4] = firstVertex + 1;
					array[num4 + 1] = firstVertex + 2;
					array[num4 + 2] = firstVertex;
					array[num4 + 3] = firstVertex + 1;
					array[num4 + 4] = firstVertex + 3;
					array[num4 + 5] = firstVertex + 2;
				}
				num4 += 6;
				firstVertex += 4;
				continue;
			}
			MeshAttachment meshAttachment = attachment as MeshAttachment;
			int num5;
			int[] triangles;
			if (meshAttachment != null)
			{
				num5 = meshAttachment.vertices.Length >> 1;
				triangles = meshAttachment.triangles;
			}
			else
			{
				SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
				if (skinnedMeshAttachment == null)
				{
					continue;
				}
				num5 = skinnedMeshAttachment.uvs.Length >> 1;
				triangles = skinnedMeshAttachment.triangles;
			}
			if (flag)
			{
				int num6 = 0;
				int num7 = triangles.Length;
				while (num6 < num7)
				{
					array[num4 + 2] = firstVertex + triangles[num6];
					array[num4 + 1] = firstVertex + triangles[num6 + 1];
					array[num4] = firstVertex + triangles[num6 + 2];
					num6 += 3;
					num4 += 3;
				}
			}
			else
			{
				int num8 = 0;
				int num9 = triangles.Length;
				while (num8 < num9)
				{
					array[num4] = firstVertex + triangles[num8];
					num8++;
					num4++;
				}
			}
			firstVertex += num5;
		}
	}
}
